package chat.server.view;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;

/**
 * 服务器窗体样式类
 *
 * #1 定义并创建服务器的窗体样式
 *
 * @author 谈笑、
 * @dateTime 2020/10/21 15:41
 */
public class ServerView {

	// 主窗体
	private JFrame jFrame = new JFrame("TCP服务器");

	// 消息记录文本显示框
	public JTextArea jTextArea;

	// 表格模型
	private DefaultTableModel defaultTableModel;

	// 在线用户显示表格
	public JTable jTable;

	// 组件容器
	private JPanel[] jPanels = {
			new JPanel(), new JPanel(), new JPanel(),
			new JPanel(), new JPanel(), new JPanel()
	};

	// 提示标签
	private JLabel[] jLabels = {
			new JLabel("消息记录"), new JLabel("在线用户"),
			new JLabel("配置服务"), new JLabel(""),
			new JLabel("本端IP地址："), new JLabel("本端端口号："),
			new JLabel("服务开启时长：服务未开启"), new JLabel("操作功能"),
			new JLabel("通知模式")
	};

	// IP、Port输入框
	public JTextField[] jTextFields = {
			new JTextField(), new JTextField()
	};


	// 消息通知模式单选框
	public JRadioButton[] jRadioButtons = {
			new JRadioButton("系统消息", true),
			new JRadioButton("全部消息")
	};


	// 操作按钮
	public JButton[] jButtons = {
			new JButton("启动服务"), new JButton("停止服务"),
			new JButton("退出程序")
	};

	// 字体
	public Font[] fonts = {
			new Font("微软雅黑", Font.PLAIN, 15),
			new Font("微软雅黑", Font.PLAIN, 16),
			new Font("微软雅黑", Font.PLAIN, 17)
	};

	// 颜色
	private Color[] colors = {
			// 按钮背景、边框
			new Color(252, 252, 252), new Color(165, 165, 165),
			// TCP、UDP模式色
			// 连接状态色
			new Color(210, 104, 104), new Color(120, 212, 105), new Color(212, 196, 106)
	};

	/**
	 * 消息记录组件容器
	 */
	public JPanel getJPanel_0() {
		// 输入框
		this.jTextArea = new JTextArea();
		this.jTextArea.setFont(this.fonts[1]);
		this.jTextArea.setText("");
		this.jTextArea.setLineWrap(true);
		this.jTextArea.setEditable(false);

		// 滚动条
		JScrollPane jScrollPane = new JScrollPane();
		jScrollPane.setBorder(null);
		jScrollPane.setViewportView(this.jTextArea);
		jScrollPane.setBounds(1, 1, 798, 258);

		// 组件容器
		this.jPanels[0].setBounds(10, 30, 800, 260);
		this.jPanels[0].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[0].setLayout(null);
		this.jPanels[0].add(jScrollPane);
		return this.jPanels[0];
	}


	/**
	 * 在线用户组件容器
	 */
	public JPanel getJPanel_1() {
		// 表头
		String[] header = {"序号", "用户名", "IP地址", "端口号", "连接时间"};
		// 设置表格模型
		this.defaultTableModel = new DefaultTableModel(new Object[][]{}, header);
		// 创建表格
		this.jTable = new JTable(this.defaultTableModel);
		// 数据字体
		this.jTable.setFont(this.fonts[0]);
		// 数据行高
		this.jTable.setRowHeight(30);
		// 禁止编辑
		this.jTable.setEnabled(false);
		// 禁止改变列位置
		this.jTable.setDragEnabled(false);

		// 表格头头样式
		JTableHeader jTableHeader = this.jTable.getTableHeader();
		// 设置表格头高度
		jTableHeader.setPreferredSize(new Dimension(this.jTable.getWidth(), 30));

		// 设置表格头字体
		jTableHeader.setFont(this.fonts[0]);

		// 单元格宽度
		this.jTable.getTableHeader().getColumnModel().getColumn(0).setPreferredWidth(10);
		this.jTable.getTableHeader().getColumnModel().getColumn(1).setPreferredWidth(110);
		this.jTable.getTableHeader().getColumnModel().getColumn(2).setPreferredWidth(50);
		this.jTable.getTableHeader().getColumnModel().getColumn(3).setPreferredWidth(30);
		this.jTable.getTableHeader().getColumnModel().getColumn(4).setPreferredWidth(120);

		// 滚动条
		JScrollPane jScrollPane = new JScrollPane(this.jTable);
		jScrollPane.setBounds(1, 1, 798, 288);

		// 组件容器
		this.jPanels[1].setBounds(10, 325, 800, 290);
		this.jPanels[1].setLayout(null);
		this.jPanels[1].setBorder(null);
		this.jPanels[1].add(jScrollPane);
		return this.jPanels[1];
	}


	/**
	 * 服务器状态指示灯组件容器
	 */
	public JPanel getJPanel_2() {
		// 指示灯
		this.jLabels[3].setBounds(10, 8, 230, 14);
		this.jLabels[3].setOpaque(true);
		this.jLabels[3].setBackground(this.colors[2]);

		// 组件容器
		this.jPanels[2].setBounds(820, 30, 250, 30);
		this.jPanels[2].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[2].setLayout(null);
		this.jPanels[2].add(this.jLabels[3]);
		return this.jPanels[2];
	}


	/**
	 * 配置服务组件容器
	 */
	public JPanel getJPanel_3() {
		// IP地址
		this.jLabels[4].setBounds(10, 15, 105, 20);
		this.jLabels[4].setFont(this.fonts[0]);

		// 端口号
		this.jLabels[5].setBounds(10, 55, 105, 20);
		this.jLabels[5].setFont(this.fonts[0]);

		// 服务计时器
		this.jLabels[6].setBounds(10, 90, 200, 20);
		this.jLabels[6].setFont(this.fonts[0]);

		// IP地址输入框
		this.jTextFields[0].setBounds(100, 13, 140,25);
		this.jTextFields[0].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jTextFields[0].setFont(this.fonts[0]);
		// 设置服务端默认绑定的IP地址为0.0.0.0即绑定所有IP地址
		this.jTextFields[0].setText("0.0.0.0");

		// 端口号输入框
		this.jTextFields[1].setBounds(100, 53, 140,25);
		this.jTextFields[1].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jTextFields[1].setFont(this.fonts[0]);
		// 设置服务端默认绑定的端口号为10000
		this.jTextFields[1].setText("8000");

		this.jPanels[3].setBounds(820, 95, 250, 120);
		this.jPanels[3].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[3].setLayout(null);
		this.jPanels[3].add(this.jLabels[4]);
		this.jPanels[3].add(this.jLabels[5]);
		this.jPanels[3].add(this.jLabels[6]);
		this.jPanels[3].add(this.jTextFields[0]);
		this.jPanels[3].add(this.jTextFields[1]);
		return this.jPanels[3];
	}


	/**
	 * 服务操作组件容器
	 */
	public JPanel getJPanel_4() {
		// 启动服务
		this.jButtons[0].setBounds(53, 20, 145, 37);
		this.jButtons[0].setFont(this.fonts[0]);
		this.jButtons[0].setBackground(this.colors[0]);
		this.jButtons[0].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jButtons[0].setFocusPainted(false);

		// 停止服务
		this.jButtons[1].setBounds(53, 67, 145, 37);
		this.jButtons[1].setFont(this.fonts[0]);
		this.jButtons[1].setBackground(this.colors[0]);
		this.jButtons[1].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jButtons[1].setFocusPainted(false);
		this.jButtons[1].setEnabled(false);

		// 退出程序
		this.jButtons[2].setBounds(53, 114, 145, 37);
		this.jButtons[2].setFont(this.fonts[0]);
		this.jButtons[2].setBackground(this.colors[0]);
		this.jButtons[2].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jButtons[2].setFocusPainted(false);

		// 组件容器
		this.jPanels[4].setBounds(820, 326, 250, 286);
		this.jPanels[4].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[4].setLayout(null);
		this.jPanels[4].add(this.jButtons[0]);
		this.jPanels[4].add(this.jButtons[1]);
		this.jPanels[4].add(this.jButtons[2]);
		return this.jPanels[4];
	}


	/**
	 * 通知模式组件容器
	 */
	public JPanel getJPanel_5() {
		// 单方通信单选框
		this.jRadioButtons[0].setBounds(25, 10, 100, 20);
		this.jRadioButtons[0].setFont(this.fonts[0]);
		this.jRadioButtons[0].setFocusPainted(false);

		// 多方通信单选框
		this.jRadioButtons[1].setBounds(140, 10, 100, 20);
		this.jRadioButtons[1].setFont(this.fonts[0]);
		this.jRadioButtons[1].setFocusPainted(false);

		// 单选按钮组
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(this.jRadioButtons[0]);
		buttonGroup.add(this.jRadioButtons[1]);

		// 组件容器
		this.jPanels[5].setBounds(820, 250, 250, 40);
		this.jPanels[5].setBorder(BorderFactory.createLineBorder(this.colors[1]));
		this.jPanels[5].setLayout(null);
		this.jPanels[5].add(this.jRadioButtons[0]);
		this.jPanels[5].add(this.jRadioButtons[1]);
		return this.jPanels[5];
	}


	/**
	 * 显示窗体
	 */
	public void display() {
		// 设置系统提示框内的字体样式
		UIManager.put("OptionPane.messageFont", new FontUIResource(this.fonts[1]));
		UIManager.put("OptionPane.buttonFont", new FontUIResource(this.fonts[0]));

		// 消息记录
		this.jLabels[0].setBounds(10, 5, 100, 20);
		this.jLabels[0].setFont(this.fonts[0]);

		// 在线用户
		this.jLabels[1].setBounds(10, 300, 100, 20);
		this.jLabels[1].setFont(this.fonts[0]);

		// 配置服务
		this.jLabels[2].setBounds(820, 70, 100, 20);
		this.jLabels[2].setFont(this.fonts[0]);

		// 操作功能
		this.jLabels[7].setBounds(820, 300, 100, 20);
		this.jLabels[7].setFont(this.fonts[0]);

		// 通知模式
		this.jLabels[8].setBounds(820, 225, 100, 20);
		this.jLabels[8].setFont(this.fonts[0]);

		this.jFrame.setSize(1086, 657);
		this.jFrame.setLayout(null);
		this.jFrame.setLocationRelativeTo(null);
		this.jFrame.setResizable(false);
		this.jFrame.add(this.jLabels[0]);
		this.jFrame.add(this.jLabels[1]);
		this.jFrame.add(this.jLabels[2]);
		this.jFrame.add(this.jLabels[7]);
		this.jFrame.add(this.jLabels[8]);
		this.jFrame.add(this.getJPanel_0());
		this.jFrame.add(this.getJPanel_1());
		this.jFrame.add(this.getJPanel_2());
		this.jFrame.add(this.getJPanel_3());
		this.jFrame.add(this.getJPanel_4());
		this.jFrame.add(this.getJPanel_5());

		this.jFrame.setVisible(true);
	}

	public JFrame getjFrame() {
		return jFrame;
	}

	public JTextArea getjTextArea() {
		return jTextArea;
	}

	public DefaultTableModel getDefaultTableModel() {
		return defaultTableModel;
	}

	public JLabel[] getjLabels() {
		return jLabels;
	}

	public JTextField[] getjTextFields() {
		return jTextFields;
	}

	public JRadioButton[] getjRadioButtons() {
		return jRadioButtons;
	}

	public JButton[] getjButtons() {
		return jButtons;
	}

	public Color[] getColors() {
		return colors;
	}

}